package gui;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import client.libmessenger.*;

public class TicTacToeWindow extends JFrame implements ActionListener, ComponentListener
{
 	protected LibMessenger libMessage = null; 
	protected String opponent = null;
	protected String username = null;
 	protected JButton[][] buttons = new JButton[3][3];
	protected JLabel lblExplanation =  new JLabel("Note: X always denotes your clicks, O always the opponent's");
	protected JPanel pnlButtons = new JPanel();
	
	public TicTacToeWindow(String username, String opponent)
	{
		super("Tic Tac Toe against "+opponent);
		
		libMessage = MessengerWindow.instance.libMessage;
		this.username = new String(username);
		this.opponent = new String(opponent);
		
		System.out.println("created new TTT window against opponent "+opponent);
		
		// Set up the GUI
		this.setLayout(new BorderLayout());
		pnlButtons.setLayout(new GridLayout(3, 3));
		
		for(int i=0; i<3; ++i)
		{
			for(int j=0; j<3; ++j)
			{
				buttons[i][j] = new JButton(" ");
				buttons[i][j].setMinimumSize(new Dimension(100, 100));
				buttons[i][j].addActionListener(this);
				pnlButtons.add(buttons[i][j]);
			} // FOR
		} // FOR
		
		this.add(lblExplanation, BorderLayout.NORTH);
		this.add(pnlButtons, BorderLayout.CENTER);
		
		addComponentListener(this);
		pack();
		this.setSize(300, 300);
	}
    public void componentMoved(ComponentEvent e) {}
    public void componentShown(ComponentEvent e) {}
    public void componentHidden(ComponentEvent e) {}
	public void componentResized(ComponentEvent e) {
        int width = getWidth();
        int height = getHeight();
        int minWidth = 300, minHeight = 300;
        
        boolean resize = false;
        if (width < minWidth) {
             resize = true;
             width = minWidth;
        }    
        if (height < minHeight) {
        	resize = true;
            height = minHeight;
        }
        if (resize)
            setSize(width, height);
     }
	
	protected String getText(int i)
	{
		int x = i / 3;
		int y = i % 3;
		return buttons[x][y].getText();
	}
	
	protected void checkGameOver()
	{
		int[][] combinations = new int[][] {{0, 1, 2},
														{2, 5, 8},
														{6, 7, 8},
														{0, 3, 6},
														{1, 4, 7},
														{3, 4, 5},
														{0, 4, 8},
														{2, 4, 6}};
		
		
		String s1, s2, s3;
		
		for(int i=0; i<8; ++i)
		{
			s1 = getText(combinations[i][0]);
			s2 = getText(combinations[i][1]);
			s3 = getText(combinations[i][2]);
			
			if(s1.equals(s2) && s2.equals(s3) && s1.trim().length() > 0)
			{
				if(s1.trim().equals("X"))
				{
					setTitle("Victory!!!");					
				}
				else
				{
					setTitle("Miserable Failure");
				}
				
				setEnabled(false);
				MessengerWindow.instance.TTTwindows.remove(this);
			} // IF
		} // FOR		
	}
	
	public void recordOpponentTick(int pos)
	{
		int x = pos / 3;
		int y =  pos % 3;
		
		buttons[x][y].setText("O");
		buttons[x][y].setEnabled(false);
		this.setTitle("Tic Tac Toe against "+opponent+": Your Turn!");
		this.setEnabled(true);
		checkGameOver();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		for(int i=0; i<3; ++i)
			for(int j=0; j<3; ++j)
				if(e.getSource() == buttons[i][j])
				{
					JButton button = buttons[i][j];
					
					if(button.getText().trim().length() == 0)
					{
						button.setText("X");
						button.setEnabled(false);
						
						libMessage.sendGameMove(this.opponent, "TTT", String.valueOf(3*i+j));
						this.setTitle("Tic Tac Toe: Waiting for "+this.opponent+"'s move");
						this.setEnabled(false);
						checkGameOver();
					}
				} // IF
	}
}
